package com.tangtang.security.service;

import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangtang.core.basic.Function;
import com.tangtang.core.basic.PagedQuery;
import com.tangtang.core.tree.Tree;
import com.tangtang.core.tree.TreeBuilder;
import com.tangtang.security.model.SysResource;
import com.tangtang.security.model.SysUser;
import com.tangtang.security.model.SysUserRoleKey;
import com.tangtang.security.view.model.SysViewResource;
import com.tangtang.security.view.model.SysViewResourceRole;

@Component
public class UserService
{
	@Autowired
	MapperContainer	container;
	
	/**
	 * 获取所有的角色资源对应关系
	 * 
	 * @return
	 */
	public List<SysViewResourceRole> getAllRoleResource()
	{
		return container.getViewMapper().getAllSysViewResourceRole();
	}
	
	/**
	 * 获取用户的菜单信息
	 * 
	 * @param userId
	 * @return
	 */
	public List<SysViewResource> getMenusByUserId(int userId)
	{
		return container.getViewMapper().getMenusByUserId(userId);
	}
	
	/**
	 * 
	 */
	private static Function<SysViewResource>	resourceFunction	= new Function<SysViewResource>()
																	{
																		@Override
																		public SysViewResource apply(List<SysViewResource> source, SysViewResource input)
																		{
																			if (input.getResourceParentId() == 0)
																			{
																				input.setDepth(0);
																				return null;
																			}
																			else
																			{
																				for (SysViewResource resource : source)
																				{
																					if (resource.getResourceId() == input.getResourceParentId())
																					{
																						input.setDepth(resource.getDepth() + 1);
																						return resource;
																					}
																				}
																				return null;
																			}
																		}
																	};
	
	private static Comparator<SysViewResource>	resourceComparator	= new Comparator<SysViewResource>()
																	{
																		@Override
																		public int compare(SysViewResource o1, SysViewResource o2)
																		{
																			if (o1.getPriority() == o2.getPriority())
																			{
																				return o1.getResourceId() - o2.getResourceId();
																			}
																			return o1.getPriority() - o2.getPriority();
																		}
																	};
	
	public List<Tree<SysViewResource>> getMenusTreeByUserId(int userId)
	{
		List<SysViewResource> list = getMenusByUserId(userId);
		return toTree(list);
	}
	
	/**
	 * 列表转换成树
	 * 
	 * @param list
	 * @return
	 */
	public List<Tree<SysViewResource>> toTree(List<SysViewResource> list)
	{
		if (list != null && list.size() > 0)
		{
			return TreeBuilder.createBuilder(list, resourceFunction, resourceComparator).build();
		}
		return null;
	}
	
	/**
	 * get resource by page
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PagedQuery<SysResource> getResource(Integer pageIndex, Integer pageSize)
	{
		PagedQuery<SysResource> page = new PagedQuery<SysResource>(pageIndex, pageSize, SysResource.class);
		List<SysResource> list = container.getViewMapper().getSysResourceByPage(page);
		page.setResult(list);
		return page;
	}
	
	/**
	 * 获取用户角色
	 * 
	 * @param userId
	 * @return
	 */
	public List<SysUserRoleKey> getRolesByUserId(int userId)
	{
		return container.getUserRoleMapper().getRolesByUserId(userId);
	}
	
	/**
	 * 根据用户名获取用户信息
	 * 
	 * @param userName
	 * @return
	 */
	public SysUser getUser(String userName)
	{
		return container.getUserMapper().getUser(userName);
	}
	
	/**
	 * @param id
	 * @return
	 */
	public SysResource getResource(Integer id)
	{
		return container.getResourceMapper().selectByPrimaryKey(id);
	}
	
	/**
	 * @param resource
	 * @return
	 */
	public boolean insertResource(SysResource resource)
	{
		return container.getResourceMapper().insert(resource) > 0;
	}
	
	/**
	 * 插入用户
	 * 
	 * @param user
	 * @return
	 */
	public boolean insertUser(SysUser user)
	{
		return container.getUserMapper().insert(user) > 0;
	}
	
	public boolean updateResource(SysResource record)
	{
		return container.getResourceMapper().updateByPrimaryKeySelective(record) > 0;
	}
}
